#!/usr/bin/env node

const { VectorSearchService } = require('./build/vector-search.js');

async function testSearchThreshold() {
  console.log('🎯 Testing Search Threshold');
  console.log('=' .repeat(35));

  const vectorService = new VectorSearchService();
  
  try {
    // 生成查询向量
    const query = "微服务架构";
    console.log(`🔍 Query: "${query}"`);
    
    const queryEmbedding = await vectorService.generateEmbedding(query);
    console.log(`✅ Generated query embedding (${queryEmbedding.length}D)`);

    // 直接使用 Qdrant 搜索，不设置阈值
    console.log('\n📊 Raw search results (no threshold):');
    const rawResults = await vectorService.qdrantClient.search('document_chunks', {
      vector: queryEmbedding,
      limit: 5,
      // 不设置 score_threshold
    });

    for (let i = 0; i < rawResults.length; i++) {
      const result = rawResults[i];
      const preview = result.payload.content.substring(0, 80) + '...';
      console.log(`${i + 1}. Score: ${result.score.toFixed(4)}`);
      console.log(`   File: ${result.payload.filePath.split('\\').pop()}`);
      console.log(`   Content: "${preview}"`);
      console.log('');
    }

    // 测试不同阈值
    const thresholds = [0.0, 0.3, 0.5, 0.7, 0.8];
    console.log('🧪 Testing different thresholds:');
    
    for (const threshold of thresholds) {
      const results = await vectorService.qdrantClient.search('document_chunks', {
        vector: queryEmbedding,
        limit: 5,
        score_threshold: threshold,
      });
      console.log(`   Threshold ${threshold}: ${results.length} results`);
    }

    // 测试其他查询
    const testQueries = [
      'Python编程',
      'TypeScript',
      '人工智能',
      'vector database',
      '机器学习'
    ];

    console.log('\n🔍 Testing various queries with threshold 0.3:');
    for (const testQuery of testQueries) {
      const testEmbedding = await vectorService.generateEmbedding(testQuery);
      const results = await vectorService.qdrantClient.search('document_chunks', {
        vector: testEmbedding,
        limit: 3,
        score_threshold: 0.3,
      });
      
      console.log(`   "${testQuery}": ${results.length} results`);
      if (results.length > 0) {
        console.log(`     Best score: ${results[0].score.toFixed(4)}`);
      }
    }

  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

if (require.main === module) {
  testSearchThreshold().catch(console.error);
}
